﻿using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;
using Mekiasm.FluidElement;
namespace Mekiasm
{
    public class MekElectrolyzer : MachineRotateAbleItem, Interface.IDynamicCraft
    {
        public static int Index;
        public int[] CraftSlotIndies => new int[0];
        public int[] FuelCraftSlotIndies => new int[] { 0 };
        public int[] ResultSlotIndies => new int[] { 1, 2 };
        public int[] RemainSlotIndies => new int[0];
        public Point2 CraftSize => new Point2(1, 1);
        public int SlotsCount => 3;
        public int BlockValue => Index;


        public MekElectrolyzer() : base("电解分离器")
        {
            
            textureSlots = new int[] { 119, 87, 81, 87, 88, 81 };
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentElectrolyzer electrolyzer = ILibrary.SubsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentElectrolyzer>();
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new ElectrolyzerWidget(electrolyzer, componentMiner);
            return base.OnInteract(blockBehavior, raycastResult, componentMiner);
        }
        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Fluid | MekBlock.DeviceType.Electric | MekBlock.DeviceType.Gas ;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z, new List<ComponentEnergyMachine.Slot>() {
                new ComponentEnergyMachine.Slot(){FuelCapacity=4096,SlotType=ComponentEnergyMachine.SlotType.IN|ComponentEnergyMachine.SlotType.CRAFT },
                new ComponentEnergyMachine.Slot(){FuelCapacity=4096,SlotType=ComponentEnergyMachine.SlotType.OUT|ComponentEnergyMachine.SlotType.RESULT|ComponentEnergyMachine.SlotType.Charge_SlotOut },
                new ComponentEnergyMachine.Slot(){FuelCapacity=4096,SlotType=ComponentEnergyMachine.SlotType.OUT|ComponentEnergyMachine.SlotType.RESULT|ComponentEnergyMachine.SlotType.Charge_SlotOut }
            }, ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Gas),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Gas),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Fluid),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Fluid)
            }), new MachineAmountLimitConfig()
            {
                MaxPower = 10240,
                InputPowerLimit = 32,
                InputFluidAmountLimit = 32,
                InputGasAmountLimit = 32,
                OutputPowerLimit = 32,
                OutputFluidAmountLimit = 32,
                OutputGasAmountLimit = 32
            });
            ElectricSystem.Add(new ElectrolyzerElement(x, y, z));
            FluidSystem.Add(new ElectrolyzerFluidElement(x, y, z));
            GasSystem.Add(new ElectrolyzerGasElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new ElectrolyzerElement(x, y, z));
            FluidSystem.Add(new ElectrolyzerFluidElement(x, y, z));
            GasSystem.Add(new ElectrolyzerGasElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ElectricSystem.Remove(new Point3(x, y, z));
            FluidSystem.Remove(new Point3(x, y, z));
            GasSystem.Remove(new Point3(x, y, z));
            ILibrary.RemoveBlockEntity(x, y, z);
        }
        
        public void GenerateRecipes(List<MekRecipe> mekRecipe)
        {
            mekRecipe.Add(new MekRecipe(0, 1, 0, 0, 2)
                .SetName("电解水").SetDescriction("电解水获得氢气和氧气")
                .SetIngredian(0, ILibrary.GetCraftId(WaterBlock.Index), 128, true, ComponentEnergyMachine.FuelType.Fliud)
                .SetResult(0, Oxygen.Index, 64, true, ComponentEnergyMachine.FuelType.Gas)
                .SetResult(1, Hydrogen.Index, 128, true, ComponentEnergyMachine.FuelType.Gas));
        }

        public void GenerateRecipeWidget(CanvasWidget canvas, ComponentEnergyMachine energyMachine, ComponentMiner miner, ref System.Action Tick)
        {
            var Component = energyMachine;
            FluidBaseProgress FluidCraft;
            FluidBaseProgress FluidResult1;
            FluidBaseProgress FluidResult2;
            ComponentEnergyMachine.Slot CraftSlot1;
            ComponentEnergyMachine.Slot ResultSlot1;
            ComponentEnergyMachine.Slot ResultSlot2;
            MekProgressWidget craftProgress = new MekProgressWidget(MekProgressWidget.ProgessType.small_right) { Size = new Vector2(80, 20), HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center, Name = "prog" };
            var inventory1 = new MekInventorySlotWidget() { VerticalAlignment = WidgetAlignment.Far, HorizontalAlignment = WidgetAlignment.Near, Name = "i1" };
            var inventory2 = new MekInventorySlotWidget() { VerticalAlignment = WidgetAlignment.Far, HorizontalAlignment = WidgetAlignment.Far, Name = "i2" };
            var inventory3 = new MekInventorySlotWidget() { VerticalAlignment = WidgetAlignment.Far, HorizontalAlignment = WidgetAlignment.Far, Name = "i3" };
            CraftSlot1 = Component.Slots[0];
            ResultSlot1 = Component.Slots[1];
            ResultSlot2 = Component.Slots[2];
            FluidCraft = new FluidBaseProgress(new Vector2(40, 120), CraftSlot1) { HorizontalAlignment = WidgetAlignment.Near, Name = "c1" };
            FluidCraft.Margin = new Vector2(30f, 0f);
            FluidResult1 = new FluidBaseProgress(new Vector2(40, 120), ResultSlot1) { Name = "r1" };
            FluidResult1.Margin = new Vector2(80f, 0f);
            FluidResult2 = new FluidBaseProgress(new Vector2(40, 120), ResultSlot2) { Name = "r2" };
            FluidResult2.Margin = new Vector2(30f, 0f);
            inventory1.AssignInventorySlot(Component, 0);inventory2.AssignInventorySlot(Component, 1);inventory3.AssignInventorySlot(Component, 2);
            if (energyMachine is ComponentCraftDemoMachine)
            {

            }
            else
            {
                inventory1.ConfigSlotMode(miner); inventory2.ConfigSlotMode(miner); inventory3.ConfigSlotMode(miner);
            }
            inventory1.Size = new Vector2(48f); inventory2.Size = new Vector2(48f); inventory3.Size = new Vector2(48f);
            inventory1.Margin = new Vector2(20f, 40f); inventory2.Margin = new Vector2(80f, 40f); inventory3.Margin = new Vector2(30f, 40f);
            inventory1.BevelColor = Color.Black;
            inventory2.BevelColor = Color.Black;
            inventory3.BevelColor = Color.Black;
            canvas.AddChildren(inventory1);
            canvas.AddChildren(inventory2);
            canvas.AddChildren(inventory3);
            canvas.AddChildren(craftProgress);
            canvas.AddChildren(FluidCraft);
            canvas.AddChildren(FluidResult2);
            canvas.AddChildren(FluidResult1);
            Tick += delegate {
                craftProgress.SetProgress(Component.CraftProgress);
            };
        }
    }
}
